﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<NStore.Domain.Product>>" %>
<%@ Import Namespace="NStore.Infrastructure.Repositories" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%
        UserRepository rep = new UserRepository();
        bool isAdmin = Request.IsAuthenticated &&
                       rep.GetByUserName(Page.User.Identity.Name).Roles.Count(r => r.Equals("admin")) == 1;
    %>
    <h2>Products</h2>

    <table>
        <tr>
            <th>Action</th>
            <%if (isAdmin){%><th>Id</th><%}%>
            <th>Name</th>
            <%if (isAdmin){%><th>Quantity</th><%}%>
            <th>Category</th>
            <th>Price</th>
        </tr>

    <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%if (isAdmin){%>
                    <%:Html.ActionLink("Edit", "Edit", new {id = item.Id})%> |
                    <%:Html.ActionLink("Delete", "Delete", new {id = item.Id})%>
                <%}%>
            </td>
            
            
            <%if (isAdmin){%>
            <td>
                <%: item.Id %>
            </td>
            <%}%>

            <td>
                <%:Html.ActionLink(item.Name, "Details", new {id = item.Id})%> 
            </td>
            
            <%if (isAdmin){%>
            <td>
                <%: item.Quantity %>
            </td>
            <%}%>

            <td>
                <%: item.Category %>
            </td>
            <td>
                <%: item.Price.ToString("0.00") %>€
            </td>
        </tr>
    
    <% } %>

    </table>
    
    <%if (isAdmin){%>
    <p>
        <%:Html.ActionLink("Create New", "Create")%>
    </p>
    <%}%>
</asp:Content>

